package com.custom.readerviewpager.View;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.viewpager.widget.ViewPager;

/**
 * @description：立体旋转
 * @Author MRyan
 * @Date 2020/3/20 14:53
 * @Version 1.0
 */
public class CustomReaderViewPager3 extends ViewPager {
    public CustomReaderViewPager3(Context context) {
        this(context, null);
    }

    public CustomReaderViewPager3(Context context, AttributeSet attrs) {
        super(context, attrs);
        setReadEffect();
    }


    @SuppressLint("ClickableViewAccessibility")
    private void setReadEffect() {
        setPageTransformer(true, new PageTransformer() {//reverseDrawingOrder 返回True前面的视图可以遮住后面的视图
            private float maxRotate = 90f; // 最大旋转角度
            @Override
            public void transformPage(View page, float position) {

                if (position < -1){
                    page.setPivotX(page.getWidth());
                    page.setPivotY(page.getHeight() / 2);
                    page.setRotationY(-maxRotate);
                }else if(position < 0){
                    page.setPivotX(page.getWidth() * (0.5f + 0.5f * (- position))); // X轴支点坐标变化范围[page.getWidth()/2, page.getWidth()]
                    page.setPivotY(page.getHeight() / 2);
                    page.setRotationY(maxRotate * position);
                }else if(position <= 1){
                    page.setPivotX(page.getWidth() * 0.5f * (1 - position)); // // X轴支点坐标变化范围[0, page.getWidth()/2]
                    page.setPivotY(page.getHeight() / 2);
                    page.setRotationY(maxRotate * position);
                }else{
                    page.setPivotX(0);
                    page.setPivotY(page.getHeight() / 2);
                    page.setRotationY(maxRotate);
                }
            }
        });
    }
}